package validadores;

import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.richfaces.component.UICalendar;

public class DataValidator implements Validator{

	@Override
	public void validate(FacesContext fc, UIComponent uic, Object val)
			throws ValidatorException {
		Locale locale = fc.getViewRoot().getLocale();  
        Application app = fc.getApplication();          
        ResourceBundle bundle = ResourceBundle.getBundle(   
        		app.getMessageBundle(), locale);        
		FacesMessage fm = null;
		String msg = null;
		UICalendar calIni = (UICalendar) uic.getAttributes().get("dataI");
		Date dataI = (Date) calIni.getValue();
		Date dataF = (Date) val;
		
		if (dataF.before(dataI)){
			msg = bundle.getString("erro_datas");
			fm = new FacesMessage(msg);
			throw new ValidatorException(fm);
		}		
	}	
}
